From: Zhi Li <lizhi1215@gmail.com>
Date: Wed, 20 Jul 2011 09:35:48 +0800
Subject: refine setcap output on errors (Closes: #633075)

The error message of setcap is a little confused. From the error message it is
hard to detect what is wrong. I removed the last 'usage' function call,
because at that code point the 'usage' can not provide any useful information.
Instead, I added a function 'mystrerror' which shall provide more information.

Last-Update: 2016-02-07
---
 progs/setcap.c | 22 +++++++++++++++++++++-
 1 file changed, 21 insertions(+), 1 deletion(-)

diff --git a/progs/setcap.c b/progs/setcap.c
index 7304343..4df1597 100644
--- a/progs/setcap.c
+++ b/progs/setcap.c
@@ -22,6 +22,25 @@ static void usage(void)
     exit(1);
 }
 
+static const char * mystrerror(int n)
+{
+  struct my_error {
+    int num;
+    const char *desp;
+  }db[] = {
+    { EINVAL, "The value of the capability argument is not permitted for a file. Or the file is not a regular (non-symlink) file" },
+    { ENODATA, "Are you removing capabilities from a file? That file does not have any capability."},
+    {0, ""}
+  };
+
+  struct my_error *p = &db[0];
+  while (p->num) {
+    if ( n == p->num ) return p->desp;
+    p++;
+  }
+  return "";
+}
+
 #define MAXCAP  2048
 
 static int read_caps(int quiet, const char *filename, char *buffer)
@@ -196,7 +215,8 @@ int main(int argc, char **argv)
 			"Failed to set capabilities on file `%s' (%s)\n",
 			argv[0], strerror(oerrno));
 		if (!explained) {
-		    usage();
+			fprintf(stderr, "%s\n", mystrerror(errno));
+			exit(1);
 		}
 	    }
 	}
